import {
    ADD_CART_COUNT,
    ADD_CART_ITEM,
    RE_CART_CHECKED,
    SUB_CART_COUNT,
    DELETE_CART
} from './mutation_type'

export default {
    addCart({state,commit},payload){
        return new Promise((res)=>{
            const cache = state.cartList.find(item=>{
                return item.iid == payload.iid
            })
            if(cache){
                commit(ADD_CART_COUNT,cache)
                res("商品数量加1~")
            }else{
                commit(ADD_CART_ITEM,payload)
                res("添加了新的商品~")
            }
        })
    },
    subCart({commit},payload){
        if(payload.counter>1){
            commit(SUB_CART_COUNT,payload)
        }
    },
    deleteCart({state,commit},payload){
        return new Promise((res)=>{
            const index = state.cartList.findIndex(item=>{
                return item.iid == payload.iid
            })
            if(index>=0){
                commit(DELETE_CART,{index})
                res('删除成功')
            }else{
                res("未查找到该元素")
            }
        })
    },
    editChecked({commit},payload){
        commit(RE_CART_CHECKED,payload)
    }
}